package com.axzhengxin.platform.afterloan.service.impl;

import com.axzhengxin.platform.afterloan.domain.FspBizApplyInfo;
import com.axzhengxin.platform.afterloan.domain.FspBizApproveInfo;
import com.axzhengxin.platform.afterloan.domain.FspBizLoanInfo;
import com.axzhengxin.platform.afterloan.domain.FspCorInf;
import com.axzhengxin.platform.afterloan.mapper.FspCorInfMapper;
import com.axzhengxin.platform.afterloan.service.IAfterLoanBizInfoService;
import com.axzhengxin.platform.afterloan.service.IFspCorInfoService;
import com.axzhengxin.platform.business.domain.BizApproveDetail;
import com.axzhengxin.platform.business.domain.BizApproveInfo;
import com.axzhengxin.platform.common.service.IDbExecService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * FspCorInfoServiceImpl
 *
 * @author jiangnan
 * @date 2021/2/23
 */
@Slf4j
@Service
public class FspCorInfoServiceImpl implements IFspCorInfoService {

    private FspCorInfMapper fspCorInfMapper;

    private IDbExecService dbExecService;

    private IAfterLoanBizInfoService fspBizInfoService;

    @Autowired
    public FspCorInfoServiceImpl(FspCorInfMapper fspCorInfMapper,
                                 IDbExecService dbExecService,
                                 IAfterLoanBizInfoService fspBizInfoService) {
        this.fspCorInfMapper = fspCorInfMapper;
        this.dbExecService = dbExecService;
        this.fspBizInfoService = fspBizInfoService;
    }

    @DataScope(deptAlias = "d", userAlias = "su")
    @Override
    public List<FspCorInf> getMyCustomerInfoList(FspCorInf query) {
        return fspCorInfMapper.selectMyCustomer(query);
    }

    @Override
    public FspCorInf getCustomerInfoById(String customerId) {
        return Optional.ofNullable(fspCorInfMapper.selectFspCorInfById(customerId))
                .map(this::fillWithIntgdata)
                .map(this::fillWithBizApplyInfo)
                .map(this::fillWithBizApproveInfo)
                .map(this::fillWithBizLoanInfo)
                .map(this::fillWithBizApproveDetail)
                .orElseThrow(() -> new RuntimeException("未查询到指定ID：" + customerId + "的客户信息"));
    }

    @Override
    public FspCorInf getCustomerInfoByName(String name) {
        return fspCorInfMapper.selectFspCorInfByName(name);
    }

    @Override
    public FspCorInf selectFspCorInfByCustomerId(String customerId) {
        return fspCorInfMapper.selectFspCorInfByCustomerId(customerId);
    }

    /**
     * 调用安徽省工商库补全企业信息
     *
     * @param fspCorInf 从业务库查询的企业信息
     * @return 补全后的企业信息
     */
    private FspCorInf fillWithIntgdata(FspCorInf fspCorInf) {
        dbExecService.getJbxxByEntName(fspCorInf.getName())
                .ifPresent(result -> {
                    fspCorInf.setEconat(result.getString("ECONAT"));
                    fspCorInf.setEntState(result.getString("STATE"));
                    fspCorInf.setOpScope(result.getString("OPSCOPE"));
                    fspCorInf.setEntType(result.getString("ENTTYPE"));
                    fspCorInf.setEntTypeName(result.getString("ENTTYPENAME"));

                    try {
                        fspCorInf.setEstDate(DateUtils.parseDate(result.getString("ESTDATE"), "yyyy-MM-dd HH:mm:ss"));
                        fspCorInf.setApprDate(DateUtils.parseDate(result.getString("APPRDATE"), "yyyy-MM-dd HH:mm:ss"));
                    } catch (ParseException e) {
                        log.error("解析日期字符串发生异常", e);
                    }
                });
        return fspCorInf;
    }

    /**
     * 查询所有相关的申请信息
     * 注意，这里会使用数据范围筛选功能，查询当前用户可视范围内的所有申请
     *
     * @param fspCorInf 从业务库查询的企业信息
     * @return 补全后的企业信息
     */
    private FspCorInf fillWithBizApplyInfo(FspCorInf fspCorInf) {
        FspBizApplyInfo bizApplyInfo = new FspBizApplyInfo();
        bizApplyInfo.setCustomerId(fspCorInf.getId());
        List<FspBizApplyInfo> fspBizApplyInfoList = fspBizInfoService.selectBizApplyInfoList(bizApplyInfo);
        fspCorInf.setBizApplyInfoList(fspBizApplyInfoList);
        return fspCorInf;
    }

    /**
     * 查询所有相关的授信信息
     * 注意，这里会使用数据范围筛选功能，查询当前用户可视范围内的所有申请
     *
     * @param fspCorInf 从业务库查询的企业信息
     * @return 补全后的企业信息
     */
    private FspCorInf fillWithBizApproveInfo(FspCorInf fspCorInf) {
        FspBizApproveInfo bizApproveInfo = new FspBizApproveInfo();
        bizApproveInfo.setCustomerId(fspCorInf.getId());
        List<FspBizApproveInfo> bizApproveInfoList = fspBizInfoService.selectBizApproveInfoList(bizApproveInfo);
        fspCorInf.setBizApproveInfoList(bizApproveInfoList);
        return fspCorInf;
    }

    /**
     * 查询所有相关的放款信息
     * 注意，这里会使用数据范围筛选功能，查询当前用户可视范围内的所有申请
     *
     * @param fspCorInf 从业务库查询的企业信息
     * @return 补全后的企业信息
     */
    private FspCorInf fillWithBizLoanInfo(FspCorInf fspCorInf) {
        FspBizLoanInfo bizLoanInfo = new FspBizLoanInfo();
        bizLoanInfo.setCustomerId(fspCorInf.getId());
        List<FspBizLoanInfo> fspBizLoanInfoList = fspBizInfoService.selectBizLoanInfoList(bizLoanInfo);
        fspCorInf.setBizLoanInfoList(fspBizLoanInfoList);
        return fspCorInf;
    }

    /**
     * 查询所有相关的放款信息
     * 注意，这里会使用数据范围筛选功能，查询当前用户可视范围内的所有申请
     *
     * @param fspCorInf 从业务库查询的企业信息
     * @return 补全后的企业信息
     */
    private FspCorInf fillWithBizApproveDetail(FspCorInf fspCorInf) {
        List<String> list = new ArrayList<>();
        List<FspBizApproveInfo> bizApproveInfoList = fspCorInf.getBizApproveInfoList();
        if (bizApproveInfoList != null && bizApproveInfoList.size() > 0) {
            bizApproveInfoList.forEach(fspBizApplyInfo -> list.add(fspBizApplyInfo.getId()));
            List<BizApproveDetail> approveDetailList = fspBizInfoService.selectBizApproveDetailList(list);
            fspCorInf.setApproveDetailList(approveDetailList);
        }
        return fspCorInf;
    }

}
